package br.edu.ufcg.lsd.wbis.ui.client.ui.setup;

import br.edu.ufcg.lsd.wbis.ui.client.beans.UserInfo;
import br.edu.ufcg.lsd.wbis.ui.client.ui.FormManager;
import br.edu.ufcg.lsd.wbis.ui.client.ui.management.PanelManager;

import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.form.Form;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.form.ValidationException;
import com.gwtext.client.widgets.form.Validator;
import com.gwtext.client.widgets.layout.AnchorLayoutData;

/**
 * @author Renato Miceli
 */
public class AddRootFormPanel extends FormPanel implements FormManager<UserInfo> {

	private static String NAME_FIELD = "name";
	private static String LOGIN_FIELD = "login";
	private static String EMAIL_FIELD = "email";
	private static String PASSWORD_FIELD = "pswd";
	private static String REPASSWORD_FIELD = "repswd";

	public AddRootFormPanel(PanelManager panelManager) {
		
		this.setLabelWidth(100);
		this.setWidth(300);
		this.setHeight(70);
		this.setFrame(true);
		this.setTitle("Create Root User");

		final TextField nameField = new TextField("Name", NAME_FIELD);
		this.add(nameField, new AnchorLayoutData("90%"));

		final TextField loginField = new TextField("Login", LOGIN_FIELD);
		this.add(loginField, new AnchorLayoutData("90%"));

		final TextField emailField = new TextField("E-mail", EMAIL_FIELD);
		emailField.setRegex(".+@.+\\.[a-z]+");
		emailField.setRegexText("Invalid email!");

		this.add(emailField, new AnchorLayoutData("90%"));

		final TextField passwordField = new TextField("Password", PASSWORD_FIELD);
		passwordField.setPassword(true);
		passwordField.setMinLength(5);
		this.add(passwordField, new AnchorLayoutData("90%"));

		final TextField retypePasswordField = new TextField("Check password", REPASSWORD_FIELD);
		retypePasswordField.setPassword(true);
		retypePasswordField.setMinLength(5);
		retypePasswordField.setInvalidText("Password does not match!");
		retypePasswordField.setValidator(new Validator() {
		
			public boolean validate(String value) throws ValidationException {
				return passwordField.getValueAsString().equals(value);
			}
		});
		this.add(retypePasswordField, new AnchorLayoutData("90%"));

		this.addButton(new Button("Register", new CreateRootUserListener(this, panelManager)));
		this.addButton(new Button("Cancel and forbid creation", new CancelAndForbidRootCreationListener(panelManager)));

		nameField.focus();
	}

	/**
	 * {@inheritDoc}
	 */
	public UserInfo parseForm() {
		UserInfo userInfo = new UserInfo();
		Form form = this.getForm();
		userInfo.setLogin(form.findField(LOGIN_FIELD).getValueAsString());
		userInfo.setPassword(form.findField(PASSWORD_FIELD).getValueAsString());
		userInfo.setRetypedPassword(form.findField(REPASSWORD_FIELD).getValueAsString());
		userInfo.setEmail(form.findField(EMAIL_FIELD).getValueAsString());
		userInfo.setName(form.findField(NAME_FIELD).getValueAsString());
		return userInfo;
	}

}
